<template>
  <el-card shadow="hover">
    <template #header>
      <el-button @click="showAddDialog" type="primary" size="default" icon="el-icon-plus">添加</el-button>
    </template>

    <el-table border :data="trademarkList">
      <el-table-column label="序号" type="index" align="center" width="80"></el-table-column>
      <el-table-column label="品牌名称" prop="tmName"></el-table-column>
      <el-table-column label="品牌LOGO">
        <template v-slot="{ row, $index }">
          <el-image :src="row.logoUrl" style="width:100px;height:60px"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="warning" size="small" icon="el-icon-edit-outline"
            @click="showUpdateDialog(scope.row)">修改</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteTrademark(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
        :page-sizes="[5, 10, 15]" :page-size="5" layout="prev, pager, next, jumper, ->, sizes, total" :total="total">
      </el-pagination>
    </div>

    <el-dialog :visible.sync="dialogFormVisible" :title="tmForm.id ? '修改品牌' : '添加品牌'">
      <el-form style="width:80%" :model="tmForm" ref="tmFormRef">
        <el-form-item label="品牌名称" label-width="100px" prop="tmName">
          <el-input v-model="tmForm.tmName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="品牌LOGO" label-width="100px" prop="logoUrl">
          <el-upload class="avatar-uploader" action="/dev-api/admin/product/upload" :show-file-list="false"
            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="addOrUpdate">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>
<script>
export default {
  data() {
    return {
      page: 1, // 当前页码
      limit: 5, // 每页数量
      total: 0, //总数
      trademarkList: [],
      dialogFormVisible: false,
      tmForm: {
        tmName: '',
        logoUrl: ''
      },
      imageUrl: '',
    }
  },
  methods: {
    // 获取页面数据
    async getPageList() {
      const page = this.page
      const limit = this.limit
      try {
        let result = await this.$API.trade.getPageList(page, limit)
        console.log(result)
        this.trademarkList = result.data.records
        this.total = result.data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 分页回调
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.page = 1
      this.limit = val
      this.getPageList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val
      this.getPageList()
    },

    // 添加品牌
    showAddDialog() {
      this.dialogFormVisible = true
      this.tmForm = {
        logoUrl: '',
        tmName: ""
      }
    },

    // 图片回调
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },

    // 确认保存
    async addOrUpdate() {
      const trademark = this.tmForm
      try {
        await this.$API.trade.addOrUpdate(trademark)
        this.$message({
          message: "成功",
          type: 'success'
        })
        this.getPageList()
        this.dialogFormVisible = false
      } catch (error) {
        console.log(error)
        this.$message({
          message: '失败分配角色成功',
          type: 'success'
        })
      }
    },

    // 删除单个
    async deleteTrademark(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$API.trade.remove(row.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getPageList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    // 修改数据
    showUpdateDialog(row) {
      this.dialogFormVisible = true
      this.tmForm = { ...row }
      console.log(row)
    }


  },
  mounted() {
    this.getPageList()
  }
}
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>